package com.yic.module.mes.controller.admin.moroute;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.mes.controller.admin.moroute.vo.*;
import com.yic.module.mes.dal.dataobject.moroute.MoRouteDO;
import com.yic.module.mes.convert.moroute.MoRouteConvert;
import com.yic.module.mes.service.moroute.MoRouteService;

@Tag(name = "管理后台 - 工单工艺路线")
@RestController
@RequestMapping("/mes/mo-route")
@Validated
public class MoRouteController {

    @Resource
    private MoRouteService moRouteService;

    @PostMapping("/create")
    @Operation(summary = "创建工单工艺路线")
    @PreAuthorize("@ss.hasPermission('mes:mo-route:create')")
    public CommonResult<Long> createMoRoute(@Valid @RequestBody MoRouteCreateReqVO createReqVO) {
        return success(moRouteService.createMoRoute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单工艺路线")
    @PreAuthorize("@ss.hasPermission('mes:mo-route:update')")
    public CommonResult<Boolean> updateMoRoute(@Valid @RequestBody MoRouteUpdateReqVO updateReqVO) {
        moRouteService.updateMoRoute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单工艺路线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mo-route:delete')")
    public CommonResult<Boolean> deleteMoRoute(@RequestParam("id") Long id) {
        moRouteService.deleteMoRoute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单工艺路线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mo-route:query')")
    public CommonResult<MoRouteRespVO> getMoRoute(@RequestParam("id") Long id) {
        MoRouteDO moRoute = moRouteService.getMoRoute(id);
        return success(MoRouteConvert.INSTANCE.convert(moRoute));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单工艺路线列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mes:mo-route:query')")
    public CommonResult<List<MoRouteRespVO>> getMoRouteList(@RequestParam("ids") Collection<Long> ids) {
        List<MoRouteDO> list = moRouteService.getMoRouteList(ids);
        return success(MoRouteConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单工艺路线分页")
    @PreAuthorize("@ss.hasPermission('mes:mo-route:query')")
    public CommonResult<PageResult<MoRouteRespVO>> getMoRoutePage(@Valid MoRoutePageReqVO pageVO) {
        PageResult<MoRouteDO> pageResult = moRouteService.getMoRoutePage(pageVO);
        return success(MoRouteConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单工艺路线 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mo-route:export')")
    @OperateLog(type = EXPORT)
    public void exportMoRouteExcel(@Valid MoRouteExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MoRouteDO> list = moRouteService.getMoRouteList(exportReqVO);
        // 导出 Excel
        List<MoRouteExcelVO> datas = MoRouteConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工单工艺路线.xls", "数据", MoRouteExcelVO.class, datas);
    }

}
